package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Depot;
import cn.wolfcode.crm.service.IDepotService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/depot")
public class DepotController {

    @Autowired
    private IDepotService depotService;

    @RequestMapping("")
    public String view(){
        return "depot";
    }

    @RequestMapping("list")
    @ResponseBody
    public List<Depot> list(){
        List<Depot> list = depotService.list();
        System.out.println(list);
        return list;
    }
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Depot d){
        JsonResult result = new JsonResult();
        try {
            depotService.save(d);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }

        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Depot d){
        JsonResult result = new JsonResult();
        try {
            depotService.update(d);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }

        return result;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            depotService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }

        return result;
    }
}
